package entities;

import akka.actor.ActorRef;
import helpers.MessageActor;
import play.db.ebean.Model;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.libs.Akka;
import scala.concurrent.duration.Duration;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created with IntelliJ IDEA.
 * User: eagami
 * Date: 01/12/13
 * Time: 00:33
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name="Station")
public class Station extends Model implements Serializable{


    /**
     *
     */
    private static final long serialVersionUID = -5469609115761087099L;
    private Long id;
    private String name;
    private Integer locationX;
    private Integer locationY;


    public static Finder<Long,Station> find = new Finder<Long,Station>(
            Long.class, Station.class
    );

    public Station() {
        // TODO Auto-generated constructor stub
    }

    @Id
    @Column(name = "Station_id")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Station_sequence")
    @SequenceGenerator(
            name="Station_sequence",
            sequenceName="dbo.Station_sequence"
    )
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    @Column(name="Name",unique=false,length=50,nullable=true)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="Location_X",unique=false,length=20,nullable=true)
    public Integer getLocationX() {
        return locationX;
    }

    public void setLocationX(Integer locationX) {
        this.locationX = locationX;
    }

    @Column(name="Location_Y",unique=false,length=20,nullable=true)
    public Integer getLocationY() {
        return locationY;
    }

    public void setLocationY(Integer locationY) {
        this.locationY = locationY;
    }

    @Transactional
    public static List<Station> getAllStations(){
        return Station.find.all();
    }
    
    public static Station getStationByID(Long stationId){
    	List l =  (List) JPA.em().createQuery("select s from Station s where s.id = "+stationId).getResultList();
    	if(l.isEmpty()){
    		return null;
    	}
    	return (Station) l.get(0);
    }

    @Transactional
    public Station saveStation() {
        return JPA.em().merge(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        Station station = (Station) o;

        if (id != null ? !id.equals(station.id) : station.id != null) return false;
        if (locationX != null ? !locationX.equals(station.locationX) : station.locationX != null) return false;
        if (locationY != null ? !locationY.equals(station.locationY) : station.locationY != null) return false;
        if (name != null ? !name.equals(station.name) : station.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = 5;;
        result = 31 * result + (id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (locationX != null ? locationX.hashCode() : 0);
        result = 31 * result + (locationY != null ? locationY.hashCode() : 0);
        return result;
    }

}
